﻿<?PHP
//验证用户权限
function GetReadRule($intRuleValue)
{
	$GetReadRule=false;
	if(!is_numeric($intRuleValue))
		return false;
	$strRules="1,3,5,9,7,11,13,15,17,19,21,23,25,27,29,31";
	$ArrRules=explode(",",$strRules);
	foreach($ArrRules as $values){
		 if($values==$intRuleValue){
		 	$GetReadRule=true;
		 }
	}
	return $GetReadRule;
}

//修改权限 4
function GetUpdateRule($intRuleValue)
{
   $GetUpdateRule=false;
   if(!is_numeric($intRuleValue)) return false;
   $strRules="4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31";
   $ArrRules=explode(",",$strRules);

   foreach($ArrRules as $v){
		if($v==$intRuleValue){
			return true;
		}
   }
   return false;

}

//添加权限 2
function GetAddRule($intRuleValue)
{

   if(!is_numeric($intRuleValue)) return false;
   $strRules="2,3,6,10,7,11,14,15,18,19,22,23,26,27,30,31";

   $ArrRules=explode(",",$strRules);
   foreach($ArrRules as $v){
   	if($intRuleValue==$v) return true;
   }
   return false;

}

//删除权限8
function GetDeleteRule($intRuleValue)
{

   if(!is_numeric($intRuleValue)) return false;
   $strRules="8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31";

   $ArrRules=explode(",",$strRules);
   foreach($ArrRules as $v)
   {
   	if($v==$intRuleValue) return true;
   }
   return false;

}
//审核权限16
function GetShenHeRule($intRuleValue)
{

   if(!is_numeric($intRuleValue)) return false;
   $strRules="16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31";

   $ArrRules=explode(",",$strRules);
   foreach($ArrRules as $v)
   {
   	if($v==$intRuleValue) return true;
   }
   return false;

}



//数据库类
class YYBDB
{

var $ArrFields;//字段列表
var  $FieldsCount;//操作字段数
var $TableName; //操作的数据表名称
var $filesConent;//欲插入的值
var $sCreateSQL; //生成的sql语句
var $countTableName;//计算字段数的表名称
var $Returns; //改变返回的影响行数
//连接数据库
function __construct()
{
  
	 global $SystemConest;
	 $hostipaddress=$SystemConest[9];
	 $mysql_dbdatabase=$SystemConest[10];
     $mysql_username=$SystemConest[11];
     $mysql_password=$SystemConest[12];

    $FieldsCount=0;
    $mysqllink=mysql_connect($hostipaddress.":".$SystemConest[13],$mysql_username,$mysql_password);
    if(!$mysqllink) die( "数据库连接失败!");
    mysql_select_db($mysql_dbdatabase,$mysqllink); //选择 数据库
    mysql_query("set names utf8");
}

//查询数据库
function db_select_query($sqlstr)
{

  return  mysql_query($sqlstr);

}
//更新数据库
function db_update_query($sqlstr)
{

  return mysql_query($sqlstr);//发送一条 MySQL 查询 返回资源类型resource

}

function db_query($sqlstr)
{

  return mysql_query($sqlstr);//执行查询语句

}

function db_fetch_array($result)
{
	return mysql_fetch_array($result);
}

//返回查询的结果行数 用于select
function db_num_rows($result)
{
	return mysql_num_rows($result) ;
}

//返回insert into update del的影响行数
function db_affected_rows()
{
	return mysql_affected_rows () ;
}


//返回模块数据库表名，暂时特定使用
function getTableName($MenuID)
{
	global $SystemTablename;
	global $SystemConest;
	$sqlrs="Select ".$SystemTablename[2]."0,".$SystemTablename[2]."9 From ". DQ.$SystemTablename[2]." Where ".$SystemTablename[2]."0=$MenuID  Order by ".$SystemTablename[2]."0";

	$rsc=$this->db_select_query($sqlrs);
	$rs=$this->db_fetch_array($rsc);
	if($this->db_num_rows($rsc)>0){
		return $rs["".$SystemTablename[2]."9"];
	}
	else
	return "";
}

//创建插入语句
   function CreateInsSQL(){
	$sqlrs="insert into ".$this->countTableName;
	for($i=1;$i<=$this->db_num_fields();$i++){//获取字段列表名称
		$sqlrs1=$sqlrs1.$this->countTableName."".$i.",";
	}
	$sqlrs1=substr($sqlrs1,0,-1);//去最后的逗号
	$sqlrs=$sqlrs." (".$sqlrs1.") values";
	$sqlrs=$sqlrs."(".$this->filesConent.")";
	$this->sCreateSQL=$sqlrs;

  }
//创建插入语句，通用
function strCreateInsSQL()
{
	$strField="";
	global $SystemConest;
	$max=max(array_keys($this->ArrFields));
  for($i=1;$i<=$max;$i++)
  {
  	if(strlen($this->ArrFields[$i])>0)
	{
		$strField=$strField .$this->TableName .$i . ",";
		
			
			$tempddd=$this->ArrFields[$i];
			if(!get_magic_quotes_gpc()){
				$tempddd=addslashes($this->ArrFields[$i]);
			}
			$values=$values."'".$tempddd."',";
		
	}

  }

  if(strlen($values)<1 || strlen($strField)<1)
  {
  	return "";
  }
  $values=substr($values,0,-1);
  $strField=substr($strField,0,-1);

  return "insert into ".$SystemConest[7].$this->TableName." ($strField) values ($values)";
}

//创建更新语句，通用
//
 function strUpdateSQL()
 {
 	global $SystemConest;
 	$strField="";
	$values="";
	$i=1;
	$n=max(array_keys($this->ArrFields));
	for($i=1;$i<=$n;$i++)

  {


	if(strlen($this->ArrFields[$i])>0)
	{

		if(is_numeric($this->ArrFields[$i])==true)
		{
			$values=$values.$this->TableName.$i."=".$this->ArrFields[$i].",";
		}
		else
		{
			$tempddd=$this->ArrFields[$i];
			if(!get_magic_quotes_gpc())
			{
				$tempddd=addslashes($this->ArrFields[$i]);
			}
			
			$values=$values.$this->TableName.$i."='".$tempddd."',";


		}
	}

  }

  if(strlen($values)<2)
  {
  	return "";
  }
  $values=substr($values,0,-1);
	 if(strlen($this->ArrFields[0])>0)
	 {

		 return "update  ".$SystemConest[7].$this->TableName." set $values where ".$this->TableName."0=".$this->ArrFields[0];
	  }
	  else
	  {
		return "update  ".$SystemConest[7].$this->TableName." set $values";
	  }
}

//创建删除语句
function strDeleteSQL()
{
	global $SystemConest;
	if(strlen($this->ArrFields[0])>0)
	{
		return "delete from ".$SystemConest[7].$this->TableName." where ".$this->TableName."0=".$this->ArrFields[0];
	}
}

 //创建更新语句
   function CreateUpdateSQL($strWhere)
  { //调用方法

  global $SystemConest;

	$sqlrs="update ".$SystemConest[7].$this->countTableName." set ";
	$filesConent_Arr=explode(","," ,".$this->filesConent);

	if($this->countTableName=="")
		$this->countTableName=$this->TableName;
	$sqlstr="select * from ".$SystemConest[7].$this->countTableName." limit 1";
	$result=$this->db_select_query($sqlstr);
	if($FieldsCount==0)
	{

		$FieldsCount=$this->db_num_fields($result)-1;
	}

	for($i=1;$i<=$FieldsCount;$i++)//获取字段列表名称
	{
		$sqlrs1=$sqlrs1.$this->countTableName."".$i."=".$filesConent_Arr[$i].",";
	}
	$sqlrs1=substr($sqlrs1,0,-1);//去最后的逗号
	$sqlrs=$sqlrs.$sqlrs1;
	$sqlrs=$sqlrs." where ".$strWhere;

	$this->sCreateSQL=$sqlrs;
  }
//创建插入语句
   function CreateInsertIntoSQL($strWhere)
  {

  }

//返回数据表字段数量
function db_num_fields($result)
{
	return mysql_num_fields($result);

}


//从结果集中取得列信息并作为对象返回
function db_fetch_field($result)
{
	return mysql_fetch_field($result);
}


//返回某个字段的类型
function db_field_type($result,$i)
{
	return mysql_field_type($result,$i);
}

//根据字段的类型更改值，以便sql语句的生成
function file_type_chaValues($result,$i,$flag,$where=" like ",$valude)
{

	if(mysql_field_type($result,$i)=="string")
	{
		return $where."'".$flag.$valude.$flag."'";
	}
	else
	{
		return "".$valude."";
	}
}


//返回配置单中MenuId所在的字段序列号
function getMenuIdNum($MenuId)
{
	global $SystemTablename;
	global $SystemConest;
	$rsc=$this->db_query("select * from ".$SystemConest[7].$SystemTablename[2]." where ".$SystemTablename[2]."0=".$MenuId);
	$rs=$this->db_fetch_array($rsc);
	if($rs[7]!="")
	{
		$arr1=explode(",",$rs[7]);
		$i=0;
		for($i=0;$i<count($arr1);$i++)
		{
			if(strstr($arr1[$i],"MenuId"))
			{
				return $i;
			}
		}
		return 0;
	}
}

//执行添加操作
function Add()
{
	$sqlrs=$this->strCreateInsSQL();
	
	if(strlen($sqlrs)>2)
	{
		$this->db_query($sqlrs);
		$this->Returns=mysql_affected_rows();
	}

}

//执行更新操作
function Update()
{
	$sqlrs=$this->strUpdateSQL();
	
	if(strlen($sqlrs)>2)
	{
		$this->db_query($sqlrs);
		$this->Returns=mysql_affected_rows();
	}
}


//执行删除操作
function Delete()
{
	$sqlrs=$this->strDeleteSQL();
	
	if(strlen($sqlrs)>2)
	{

		$this->db_query($sqlrs);
		$this->Returns=mysql_affected_rows();
	}

}

//关闭数据库
function db_close()
{
   if(!$this->db_close())
   {
    mysql_close();
   }
}
}

class RsPage extends YYBDB
{
//分页类
//在用new新建类时必须传入下面几个参数
//$page,$numShowStyle,$sql,pagecount


/** 用法实例
mypage=new RsPage();
$sqlstr="";查询的sql语句,不要带limit
$mypage->pagecount=$fanyenum;
$mypage->StrPage="p"; 翻页的变量名
$mypage->otherUrl="";//若有值请以"&"开始
$mypage->numShowStyle=0;//显示分页样式
$mypage->sql=$mypage->FenyeSql($sqlstr);
$bsnum=$mypage->ReturnBeginNumber();//用于那个循环的sql语句的起始记录,$bsnum用于具体翻页时的起始数
$rsc=$mypage->db_query($sqlstr." limit $bsnum,".$mypage->pagecount);  //这里的$rsc就是获取到了的翻页记录集

要显示翻页的tml代码：echo $mypage->showFenPage();
*/


	var $numPage;//共有多少页
	var $page;//初始化时默认第几页
	var $pagecount;//每页多少条记录
	var $sWhereSql;//查询条件
	var $recordcount;//共有多少条记录
	var $homepage;//首页
	var $nextPage;//下页
	var $previous;//上一页
	var $lastPage;
	var $numShowStyle;//显示样式
	var $demo;//临时变量
	var $begin;
	var $TableName;//表名
	var $StrPage; //获取分页样式名称 比如p
	var $otherUrl;// 附带其它URL参数
	var $sql= "";

	function Load()
	{

		//$sqlrs="select count(*) as countnum from ".$this->TableName." where ".$this->sWhereSql;

		$rsc=$this->db_query($this->sql);
		
		$this->rsc=$this->db_fetch_array($rsc);

		$this->recordcount=$this->rsc["countnum"];
		$this->page=$_GET[$this->StrPage];
		if($this->page=="" or $this->page=="0" ) {
			$this->page="1";
			
		  }
		
		if(!intval($this->page))
		{
			die("请不要修改页面参数");
		}
		

		  //确定共有多少页
		if($this->recordcount % $this->pagecount >0)
		{
			$this->numPage=(int)($this->recordcount / $this->pagecount)+1;
		}
		else
		{
			$this->numPage=(int)($this->recordcount / $this->pagecount);
		}

		//获取的页面大于总页数时就为总页数
		if((int)($this->page)>$this->numPage)
		{
			$this->page=$this->numPage;
		}
		$this->page=(int)($this->page);
		
		


	}

	function showFenPage()
	{
		switch($this->numShowStyle)
		{
			case 0:
				return $this->fenlei1();
				break;
		}
	}

	function FenyeSql($sql)
	{//把查询sql替换成能分页的sql语句
		if($sql!="")
		{
			return str_replace("*","count(*) as countnum",$sql);
		}
		else
		{
			return "";
		}

	}

	function ReturnBeginNumber()
	{
		
		$this-> Load();
		
		$r=(($this->page)-1)*$this->pagecount;
		if($r<0){
			return 0;
		}
		else{
			return $r;
		}
	}

	//分类样式
	function fenlei1()
	{
		
		
		if($this->page<>1){
			$this->homepage="<a href='?".$this->StrPage."=1".$this->otherUrl."'>首页</a> | ";
		}

		if($this->page>1){
			$this->demo=$this->page-1;
			$this->previous="<a href='?".$this->StrPage."=".$this->demo .$this->otherUrl."'>上一页</a> | ";
		}
		if($this->page<$this->numPage){
			$this->demo=$this->page+1;
			$this->nextPage="<a href='?".$this->StrPage."=".$this->demo .$this->otherUrl."'>下一页</a> | ";
		}

		if($this->page<$this->numPage)
		{
			$this->demo=$this->numPage;
			$this->lastPage="<a href='?".$this->StrPage."=".$this->demo .$this->otherUrl."'>末页</a>";
		}

		return "<div  style='margin-right:30px;'>共有".$this->recordcount."条记录,每页".$this->pagecount."条记录,共分".$this->numPage."页,".$this->homepage.$this->previous.$this->nextPage.$this->lastPage."  现在是第".$this->page."页</div>";
	}
}
?>

